/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.infor.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.infor.entity.UpCustomers;
import com.jeeplus.modules.infor.mapper.UpCustomersMapper;
import com.jeeplus.modules.infor.entity.TakeAddress;
import com.jeeplus.modules.infor.mapper.TakeAddressMapper;

/**
 * 上游客户Service
 * @author 李超辉
 * @version 2017-11-22
 */
@Service
@Transactional(readOnly = true)
public class UpCustomersService extends CrudService<UpCustomersMapper, UpCustomers> {

	@Autowired
	private TakeAddressMapper takeAddressMapper;
	@Autowired
	private ProjectService projectService;
	public UpCustomers get(String id) {
		UpCustomers upCustomers = super.get(id);
		upCustomers.setTakeAddressList(takeAddressMapper.findList(new TakeAddress(upCustomers)));
		return upCustomers;
	}
	
	public List<UpCustomers> findList(UpCustomers upCustomers) {
		return super.findList(upCustomers);
	}
	
	public Page<UpCustomers> findPage(Page<UpCustomers> page, UpCustomers upCustomers) {
		return super.findPage(page, upCustomers);
	}
	
	@Transactional(readOnly = false)
	public void save(UpCustomers upCustomers) {
		super.save(upCustomers);
		for (TakeAddress takeAddress : upCustomers.getTakeAddressList()){
			if (takeAddress.getId() == null){
				continue;
			}
			if (TakeAddress.DEL_FLAG_NORMAL.equals(takeAddress.getDelFlag())){
				if (StringUtils.isBlank(takeAddress.getId())){
					takeAddress.setUpCustomers(upCustomers);
					takeAddress.setPro(projectService.get(upCustomers.getProject()).getName());
					takeAddress.preInsert();
					takeAddressMapper.insert(takeAddress);
				}else{
					takeAddress.preUpdate();
					takeAddressMapper.update(takeAddress);
				}
			}else{
				takeAddressMapper.delete(takeAddress);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(UpCustomers upCustomers) {
		super.delete(upCustomers);
		takeAddressMapper.delete(new TakeAddress(upCustomers));
	}
	
}